// pages/articledetail/articledetail.js
var WxParse = require('../../resource/wxParse/wxParse.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page:1,
    hasMore:true,
    showhf:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var id = options.id;
    var USER_ID = wx.getStorageSync("USER_ID")
    var that = this;
    this.setData({
      id:id,
      USER_ID:USER_ID
    });
    this.loadComment();
    if(!wx.getStorageSync("USER_ID")){
      wx.showModal({
        title: '提示',
        content: '检测到您为未注册用户，将不能进行评论及点赞行为！:）',
      });
      // return false;
    }
    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/findCartNestId',
      data:{
        USER_ID: USER_ID,
        CATNEST_ID:id
      },
      success:function(res){
        console.log(res.data);
        if(res.data.code!=1){
          wx.showModal({
            title: '提示',
            content: data.message,
          });
          return false;
        }

        that.setData({
          arti:res.data.data
        });

        var article = res.data.data.MESSAGE,
          imgs = res.data.data.FIMG,
          html = '';
        if(article.indexOf("/style>")>0){
          article = (article.split("/style>"))[1];
        }

        WxParse.wxParse('article', 'html', article, that,5);

        console.log(res.data.data.MESSAGE)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    console.log(this.data.id);
    return {
      title: this.data.arti.SUBJECT,
      path: '/pages/articledetail/articledetail?id='+this.data.id,
      success: function (res) {
        wx.showToast({
          title: '转发成功！',
        })
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },

  clickzan:function(){
    var obj = this.data.arti;
    if (!wx.getStorageSync("USER_ID")) {
      wx.showModal({
        title: '提示',
        content: '检测到您为未注册用户，将不能进行评论及点赞行为！:）',
      });
      return false;
    }
    if(obj.ZAN==0){//未赞
      obj.ZAN=1;
      obj.VIEWS++;
      this.setData({
        arti:obj
      })
    }else{//已赞
      obj.ZAN =0;
      obj.VIEWS--;
      this.setData({
        arti: obj
      })
    }
    

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/ZanCatnest',
      data:{
        CATNEST_ID:this.data.id,
        USER_ID:this.data.USER_ID,
        STATUS: obj.ZAN == 0?0:1
      },
      success:function(){

      }
    })
  },


  loadComment:function(){
    wx.showLoading({
      title: '加载中...',
      mask:true
    })
    var id = this.data.id,
    USER_ID = wx.getStorageSync("USER_ID"),
    page = this.data.page,
    clist = this.data.clist;
    var that = this

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/findCartNestComment',
      data:{
        CATNEST_ID:id,
        pageNum:page,
        USER_ID:USER_ID,
      },
      success:function(res){
        wx.hideLoading();
        var data = res.data;
        console.log(data);
        if(data.code!=1){
          wx.showModal({
            title: '提示',
            content: data.message,
          });
          return false;
        }else{
          if(!data.data.object||data.data.object.length<=0){
            that.setData({
              hasMore: false
            })
            return false;
          }
          var newlist = data.data.object;
          var slist = [];
          if(page==1){
            for(var i=0;i<newlist.length;i++){
              slist.push(newlist[i]);
            }
            that.setData({
              clist: slist,
              page:++page
            })
          }else{
            for (var i = 0; i < newlist.length; i++) {
              clist.push(newlist[i]);
            }
            that.setData({
              clist: clist,
              page: ++page
            })
          }

          
        }
      }
    })

    
  },

  comzan:function(e){

    if (!wx.getStorageSync("USER_ID")) {
      wx.showModal({
        title: '提示',
        content: '检测到您为未注册用户，将不能进行评论及点赞行为！:）',
      });
      return false;
    }
    var comlist = this.data.clist,
    idx = e.currentTarget.dataset.idx,
    id = e.currentTarget.dataset.id;
    if(comlist[idx].ZAN==0){//未赞
      comlist[idx].ZAN=1;
      comlist[idx].VIEWS++;
    }else{//已赞
      comlist[idx].ZAN = 0;
      comlist[idx].VIEWS--;
    }
    this.setData({
      clist:comlist
    });


    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/ZanComentCatnest',
      data: {
        COMMENT_CATNEST_ID: id,
        USER_ID: this.data.USER_ID,
        STATUS: comlist[idx].ZAN == 0 ? 0 : 1
      },
      success: function () {

      }
    })
  },

  sendcom:function(e){
    var val = e.detail.value;
    val = val.replace(/\s/g,"");
    var that = this;

    if (!wx.getStorageSync("USER_ID")) {
      wx.showModal({
        title: '提示',
        content: '检测到您为未注册用户，将不能进行评论及点赞行为！:）',
      });
      return false;
    }

    wx.request({
      url: 'https://m.nongjike.cn/NJK/app/CommentXCartNest',
      method:"POST",
      
      data:{
        USER_ID:this.data.USER_ID,
        CATNEST_ID:this.data.id,
        MESSAGE:val,
        img1:'',
        img2:'',
        img3:'',
        count:0
      },
      header: {
        'content-type': "application/x-www-form-urlencoded"
      },
      success:function(res){
        console.log(res);
        if(res.data.code==1){
          wx.showToast({
            title: res.data.message,
          });
          that.setData({
            page:1,
          })
          that.loadComment();
        }
      }
    })
  },

  huifu1:function(e){
    var data = e.currentTarget.dataset;
    var id1 = data.ido,
      name = data.n;
    if (!wx.getStorageSync("USER_ID")) {
      wx.showModal({
        title: '提示',
        content: '检测到您为未注册用户，将不能进行评论及点赞行为！:）',
      });
      return false;
    }

    this.setData({
      showhf:true,
      hfobj:{
        type:1,
        name:name,
        id1:id1
      }
    })
  },

  huifu2: function (e) {
    var data = e.currentTarget.dataset,
    id1 = data.ido,
    id2 = data.idt,
    name = data.n;
    if (!wx.getStorageSync("USER_ID")) {
      wx.showModal({
        title: '提示',
        content: '检测到您为未注册用户，将不能进行评论及点赞行为！:）',
      });
      return false;
    }


    this.setData({
      showhf: true,
      hfobj:{
        type:2,
        id1:id1,
        id2:id2,
        name:name
      }
    })
  },

  hidehf:function(){
    this.setData({
      showhf:false
    })
  },

  huifu:function(e){
    var src = this.data.hfobj.type == 1 ? 'https://m.nongjike.cn/NJK/app/CommentCartNestHuiFu' :"https://m.nongjike.cn/NJK/app/CommentCartNestHuiFus",
    val = (e.detail.value),
    that = this,
      data = this.data.hfobj.type == 1?{
        CATNEST_ID:this.data.id,
        MESSAGE:val,
        PUSER_ID:'',
        COMMENT_CATNEST_ID: this.data.hfobj.id1,
        USER_ID:this.data.USER_ID
      }:{
          CATNEST_ID: this.data.id,
          MESSAGE: val,
          PUSER_ID: this.data.hfobj.id1,
          PID: this.data.hfobj.id2,
          USER_ID: this.data.USER_ID
      };
      if(val.length<=0){
        wx.showModal({
          title: '提示',
          content: '回复内容不能为空！',
        });
        return false;
      }

      this.setData({
        hfobj:{},
        showhf:false
      })

      if (!wx.getStorageSync("USER_ID")) {
        wx.showModal({
          title: '提示',
          content: '检测到您为未注册用户，将不能进行评论及点赞行为！:）',
        });
        return false;
      }

      wx.request({
        url: src,
        method: "POST",
        header:{
          "content-type":"application/x-www-form-urlencoded"
        },
        data:data,
        success:function(res){
          var data = res.data;
          console.log(data);
          if(data.code==1){
            that.setData({
              page:1
            })
            that.loadComment();
          }
        }
      })
  }
})